﻿using KTD.Web.Models;
using KTD.Web.Models.VD;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using XXS.Common.BaseDataAccessLayer;
using XXS.Common.Paginate;

namespace KTD.Web.BLL
{
    public class CustomerRecordManager
    {

        private readonly IBaseDAL<CustomerRecord> _CustomerRecordDal;
        private readonly IBaseDAL<Member> _MemberDal;
        private readonly IBaseDAL<House> _HouseDal;
        public CustomerRecordManager()
        {
            _CustomerRecordDal = new BaseDAL<CustomerRecord>(new DB());
            _HouseDal = new BaseDAL<House>(new DB());
            _MemberDal= new BaseDAL<Member>(new DB());
        }

        /// <summary>
        /// 获取用户备案列表
        /// </summary>
        /// <param name="im"></param>
        /// <returns></returns>
        public virtual IPaginate<CustomerRecordVD> GetListBy(CustomerRecordQD im)
        {
            var res = _CustomerRecordDal.FindList();
            if (im.MemberId!=null)
            {
                res = res.Where(n => n.MemberId == im.MemberId);
            }
          
            var list = res.OrderByDescending(n => n.CreateDateTime)
                .ToPaginate(im, n => new CustomerRecordVD().MapFrom(n));
            return list;
        }

        /// <summary>
        /// 新建用户备案
        /// </summary>
        /// <param name="im"></param>
        public void Create(CustomerRecordCUD im)
        {
            CustomerRecord model = new CustomerRecord();
            

            im.MapTo(model);

            model.HouseIds = string.Join(",", im.Houses.Select(n => n.Id.ToString()));
            model.HouseNames = string.Join(",", im.Houses.Select(n => n.Name+":"
            +n.SalerNames+":"+n.SalerTels ));
            model.MemberId = im.MemberId;
            model.Brokers = string.Join(",", im.Brokers.Select(n => n.Name + ':' + n.Tel));
            model.Customers = string.Join(",", im.Customers.Select(n => n.Name + ':' + n.Tel));
            var admins = _MemberDal.FindList(n => n.Type == "管理员").ToList();
            var adminTel = "";
            foreach (var admin in admins)
            {
                if (!string.IsNullOrEmpty(admin.Mobile))
                {
                    adminTel = adminTel + admin.Mobile + ",";
                }
            }
            adminTel = adminTel.TrimEnd(',');
            MF.MessageManager.sendMessage(adminTel, "SMS_187934629", "{dizhi:'" + model.HouseNames.Split(':')[0] + "',kehu:'" + model.Customers.Split(':')[0] + "',jingjiren:'" + model.Brokers.Split(':')[0] + "',shijian:'" + model.DateTime.ToString() + "'}");
            _CustomerRecordDal.Create(model);
        }
        /// <summary>
        /// 查找
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public CustomerRecordCUD Find(int id)
        {
            CustomerRecord model = _CustomerRecordDal.Find(id);
            return new CustomerRecordCUD().MapFrom(model);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        public void Delete(int id)
        {
            CustomerRecord model = _CustomerRecordDal.Find(id);
            if (model == null)
            {
                throw new Exception("错误的ID");
            }
            _CustomerRecordDal.Delete(model);
        }


        /// <summary>
        /// 审核备案
        /// </summary>
        /// <param name="im"></param>
        public void Update(CustomerRecordUD im)
        {
          
            if (im.Ids!=null&&im.Ids.Count != 0)
            {
                foreach (var id in im.Ids)
                {
                    var model = _CustomerRecordDal.Find(id);
                    model.Status = im.Status;
                    _CustomerRecordDal.Update(model);
                }
            }
            
        }
    }
}